package com.suep.game_chat.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suep.game_chat.entity.User;
import com.suep.game_chat.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户数据访问层
 * 提供额外的数据访问方法，补充UserMapper的功能
 */
@Repository
public class UserRepository {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户名查找用户
     * @param username 用户名
     * @return 用户对象
     */
    public User findByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据游戏UID查找用户
     * @param gameUid 游戏UID
     * @return 用户对象
     */
    public User findByGameUid(String gameUid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("game_uid", gameUid);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据邮箱查找用户
     * @param email 邮箱
     * @return 用户对象
     */
    public User findByEmail(String email) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据手机号查找用户
     * @param phone 手机号
     * @return 用户对象
     */
    public User findByPhone(String phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 分页查询用户列表
     * @param page 页码
     * @param size 每页大小
     * @return 用户列表
     */
    public List<User> findAll(int page, int size) {
        Page<User> pageParam = new Page<>(page, size);
        return userMapper.selectPage(pageParam, null).getRecords();
    }

    /**
     * 根据等级范围查询用户
     * @param minLevel 最小等级
     * @param maxLevel 最大等级
     * @return 用户列表
     */
    public List<User> findByLevelRange(int minLevel, int maxLevel) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("level", minLevel, maxLevel);
        return userMapper.selectList(queryWrapper);
    }

    /**
     * 查询最近注册的用户
     * @param days 天数
     * @return 用户列表
     */
    public List<User> findRecentlyRegistered(int days) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("register_time", LocalDateTime.now().minusDays(days));
        return userMapper.selectList(queryWrapper);
    }

    /**
     * 查询最近登录的用户
     * @param days 天数
     * @return 用户列表
     */
    public List<User> findRecentlyLoggedIn(int days) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("last_login_time", LocalDateTime.now().minusDays(days));
        return userMapper.selectList(queryWrapper);
    }

    /**
     * 根据状态查询用户
     * @param status 状态
     * @return 用户列表
     */
    public List<User> findByStatus(Integer status) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        return userMapper.selectList(queryWrapper);
    }

    /**
     * 更新用户最后登录时间
     * @param userId 用户ID
     * @param lastLoginTime 最后登录时间
     */
    public void updateLastLoginTime(String userId, LocalDateTime lastLoginTime) {
        User user = new User();
        user.setUserId(userId);
        user.setLastLoginTime(lastLoginTime);
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(user);
    }

    /**
     * 更新用户经验值和等级
     * @param userId 用户ID
     * @param experience 经验值
     * @param level 等级
     */
    public void updateExperienceAndLevel(String userId, Integer experience, Integer level) {
        User user = new User();
        user.setUserId(userId);
        user.setExperience(experience);
        user.setLevel(level);
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(user);
    }
}
